package com.tywj.buscustomerapp.view.activity;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.model.bean.ProblemBean;
import com.tywj.buscustomerapp.utils.WebServicrUtils;
import com.tywj.buscustomerapp.utils.myadapter.CommonRcvAdapter;
import com.tywj.buscustomerapp.utils.myadapter.item.AdapterItem;
import com.tywj.buscustomerapp.view.item.ActItme;
import com.umeng.analytics.MobclickAgent;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ActCenterActivity extends BaseActivity {
    @BindView(R.id.head_left)
    ImageView back;
    @BindView(R.id.head_title)
    TextView title;
    @BindView(R.id.recycler)
    RecyclerView recyclerView;
    @BindView(R.id.refresh)
    SmartRefreshLayout srl;
    @BindView(R.id.error_null)
    RelativeLayout nothong;
    private CommonRcvAdapter<ProblemBean> adapter;
    private List<ProblemBean> list = new ArrayList<>();

    @Override
    protected int getContentId() {
        return R.layout.activity_act_center;
    }

    @Override
    protected void initWidget(Bundle savedInstanceState) {
        super.initWidget(savedInstanceState);
        title.setText("活动中心");
        adapter = new CommonRcvAdapter<ProblemBean>(list) {
            @NonNull
            @Override
            public AdapterItem createItem(Object type) {
                return new ActItme();
            }
        };
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        super.initData();
        srl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                Observable.create(new ObservableOnSubscribe<List<ProblemBean>>() {
                    @Override
                    public void subscribe(ObservableEmitter<List<ProblemBean>> e){
                        try {
                            List<ProblemBean> beans=  WebServicrUtils.getInstance().getActivitys();
                            if (beans!=null && beans.size()>0){
                                e.onNext(beans);
                            }else{
                                e.onError(null);
                            }
                        } catch (IOException e1) {
                            e1.printStackTrace();
                            e.onError(null);
                        } catch (XmlPullParserException e1) {
                            e1.printStackTrace();
                            e.onError(null);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }
                }).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<List<ProblemBean>>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(List<ProblemBean> s) {
                                if (adapter!=null) {
                                    adapter.setData(s);
                                    adapter.notifyDataSetChanged();
                                }
                                if (srl!=null)
                                    srl.finishRefresh();
                            }

                            @Override
                            public void onError(Throwable e) {
                                nothong.setVisibility(View.VISIBLE);
                            }

                            @Override
                            public void onComplete() {

                            }
                        });
            }
        });
        srl.autoRefresh();

    }

    @Override
    protected void initClick() {
        super.initClick();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("活动中心");
        MobclickAgent.onResume(this); //统计时长

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("活动中心");
        MobclickAgent.onPause(this);
    }
}
